﻿using CBS.APP;
using CBS.DB.CBSEntity;
using CBS.Model.UIModel;
using CBS.Model.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace CBS.Desktop.FormPage
{
    /// <summary>
    /// AddLesson.xaml 的交互逻辑
    /// </summary>
    public partial class AddLesson : Window
    {
        public AddLesson()
        {
            InitializeComponent();

            BindSemester();
            BindTeacher();

            //tb_StartTime.SelectedDateTime = DateTime.Now.Date.AddHours(DateTime.Now.Hour).AddMinutes(DateTime.Now.Minute);
            //tb_EndTime.SelectedDateTime = DateTime.Now.Date.AddHours(DateTime.Now.Hour).AddMinutes(DateTime.Now.Minute);
        }

        private async void BindSemester()
        {
            try
            {
                var list = await Task.Run(() => SemesterApp.GetSelectList());

                if (list.Count == 0) return;

                this.Dispatcher.Invoke(new Action(() =>
                {
                    cb_Semester.ItemsSource = list;
                }));
                cb_Semester.SelectedIndex = 0;
                //BindClassPlan();
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, "发生错误！", MessageBoxButton.OK, MessageBoxImage.Error);
            }

        }

        private async void BindCoursePlan()
        {
            try
            {
                int semesterId = ((SemesterSelectModel)cb_Semester.SelectedItem).SemesterId;
                var list = await Task.Run(() => CoursePlanApp.GetFullDataSelectList(semesterId));

                if (list.Count == 0) return;

                this.Dispatcher.Invoke(new Action(() =>
                {
                    cb_CoursePlan.ItemsSource = list;
                }));
                cb_CoursePlan.SelectedIndex = 0;
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, "发生错误！", MessageBoxButton.OK, MessageBoxImage.Error);
            }

        }

        private async void BindTeacher()
        {
            try
            {
                var list = await Task.Run(() => TeacherApp.GetSelectList());

                if (list.Count == 0)
                {
                    return;
                }

                this.Dispatcher.Invoke(new Action(() =>
                {
                    cb_Teacher.ItemsSource = list;
                }));
                cb_Teacher.SelectedIndex = 0;
                //BindClassPlan();
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, "发生错误！", MessageBoxButton.OK, MessageBoxImage.Error);
            }

        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            LessonViewModel lesson = new();
            int coursePlanId = int.Parse(cb_CoursePlan.SelectedValue.ToString());
            int teacherId = int.Parse(cb_Teacher.SelectedValue.ToString());
            decimal lessonTimeLong = decimal.Parse(tb_LessTimeLong.Text);
            List<DateTime> startTime = string.IsNullOrWhiteSpace(tb_StartTime.Text) ? null : tb_StartTime.Text.Trim().Replace("：", ":").Split("\r\n").ToList().ConvertAll(t => Convert.ToDateTime(t));

            try
            {
                if (LessonApp.CreateLesson(coursePlanId, teacherId, lessonTimeLong, startTime))
                {
                    this.DialogResult = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, "发生错误！", MessageBoxButton.OK, MessageBoxImage.Error);
            }

        }

        private void cb_Semester_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            BindCoursePlan();
        }

        private void cb_CoursePlan_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int coursePlanId = ((CoursePlanSelectModel)cb_CoursePlan.SelectedItem).CoursePlanId; //int.Parse(cb_CoursePlan.SelectedValue.ToString());
            var plan = CoursePlanApp.GetForm(coursePlanId);
            tb_LessTimeLong.Text = plan.ClassTimeLong.ToString();
        }

    }
}
